package cn.lanqiao.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cn.lanqiao.dao.RegisterDao;
import cn.lanqiao.entity.Register;
import cn.lanqiao.util.JdbcUtil;

public class RegisterDaoImpl implements RegisterDao {
	@Override
	public int writerUser(cn.lanqiao.entity.Register register) {
		Connection conn = JdbcUtil.getConnection();
		int affter =0;
		String sql = "insert into USERS (USERID,USERNAME,ACCOUNT,PASSWORD) values (?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, register.getUserid());
			ps.setString(2, register.getUserName());
			ps.setString(3, register.getAccount());
			ps.setString(4, register.getPassword());
			affter = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JdbcUtil.close(null, ps, conn);
		}
		return affter;
	}

	@Override
	public Register checkAccount(String account) {
		Connection conn = JdbcUtil.getConnection();
		String sql = "select * from users where account = ?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		Register register = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, account);
			rs = ps.executeQuery();
			if(rs.next()) {
				register  = new Register(rs.getString("userid"),rs.getString("username"),rs.getString("account"),rs.getString("password"));
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JdbcUtil.close(rs, ps, conn);
		}
		return register;
	}
}
